<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>edit</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input id="username" type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            <tip>填写账号的昵称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input id="password" type="text" name="username" lay-reqtext="密码不能为空" placeholder="请输入密码" value="" class="layui-input">
            <tip>填写账号的密码(不填默认不修改)。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男" checked="">
            <input type="radio" name="sex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input id="email" type="email" name="email" placeholder="请输入邮箱" value="" class="layui-input">
        </div>
    </div>
    <input id="id" type="text" name="id" hidden>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>

    //接收父页面传递过来的参数
    function child(data) {
        $("#username").val(data.username);
        $("#email").val(data.email);
        $("#phone").val(data.phone);
        $("#id").val(data.id);
    }

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var id = $("#id").val();
            var username = $("#username").val();
            var email = data.field.email;
            var password = data.field.password;
            var sex = data.field.sex;
            var phone = data.field.phone;

            $.ajax({
                url: "/user/edit",//请求地址
                dataType: "json",//数据格式
                type: "PUT",//请求方式
                async: false,//是否异步请求
                data:{
                    "id":id,
                    "email":email,
                    "username":username,
                    "password":password,
                    "sex":sex,
                },
                success: function (data) {   //如何发送成功
                    layer.msg(data.msg);
                },
                error: function (data) {
                    layer.msg("服务器异常，请联系管理员!");
                }
            })

            return false;
        });

    });
</script>
</body>
</html>
